/**
 * 
 */
package com.depot.auth.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import com.base.KeyGenerator;
import com.depot.domains.UserRole;
import com.soar.context.Environment;
import com.soar.jdbc.dbhelper.DbParameter;
import com.soar.jdbc.dbhelper.DbTemplates;
import com.soar.jdbc.dbhelper.SameTypes;
import com.soar.jdbc.dbhelper.SortTypes;
import com.soar.jdbc.dbhelper.SqlTypes;
import com.soar.jdbc.dbhelper.WhereTypes;
import com.soar.util.StringUtils;

/**
 * 
 * @author Gravey Quen
 * @version $Revision$ - $Author$ at $Date$
 * @description com.itaskstar.auth.dao.UserRoleDao
 */
public class UserRoleDao
{
	/** *用户角色列表 * @param env * @param domain * @return * @throws Exception */
	public static List<HashMap<String, String>> select(Environment env, UserRole domain) throws SQLException
	{
		if (StringUtils.isEmpty(domain.getUserIds()) || domain.getUserIds().indexOf(",") != -1)
		{
			DbParameter param = new DbParameter("APP_ROLE T1");
			StringBuilder sb = new StringBuilder();
			sb.append("T1.ID,");
			sb.append("T1.ROLE_NAME,");
			sb.append("T1.ROLE_CODE,");
			sb.append("T1.REMARK,");
			sb.append("T1.CREATE_USER,");
			sb.append("T1.CREATE_USER_ID,");
			sb.append("T1.CREATE_DATE,");
			sb.append("T1.UPDATE_USER,");
			sb.append("T1.UPDATE_USER_ID,");
			sb.append("T1.UPDATE_DATE");
			param.setSelectFields(sb.toString());
			param.setOrderParams("T1.ID", SortTypes.ASC);
			return DbTemplates.select(env.getConnection(), param);
		}
		else
		{
			DbParameter param = new DbParameter("APP_ROLE T1 LEFT JOIN USER_ROLE T2 ON T2.ROLE_ID = T1.ID AND T2.USER_ID='" + domain.getUserIds() + "'");
			StringBuilder sb = new StringBuilder();
			sb.append("T1.ID,");
			sb.append("T1.ROLE_NAME,");
			sb.append("T1.ROLE_CODE,");
			sb.append("T1.REMARK,");
			sb.append("T1.CREATE_USER,");
			sb.append("T1.CREATE_USER_ID,");
			sb.append("T1.CREATE_DATE,");
			sb.append("T1.UPDATE_USER,");
			sb.append("T1.UPDATE_USER_ID,");
			sb.append("T1.UPDATE_DATE");
			param.setSelectFields(sb.toString() + " ,COUNT(T2.ID) CT");
			param.setGroup(sb.toString());
			param.setOrderParams("ID", SortTypes.ASC);
			return DbTemplates.select(env.getConnection(), param);
		}
	}

	/**
	 * 给用户分配角色
	 * 
	 * @param env
	 * @param domain
	 * @return
	 * @throws SQLException
	 */
	public static String doSave(Environment env, UserRole domain) throws SQLException
	{
		String[] uids = domain.getUserIds().split(",");

		DbParameter param = new DbParameter("USER_ROLE");
		param.setWhereParams("USER_ID", param.getInSQL(uids), SqlTypes.CUSTOM, WhereTypes.AND, SameTypes.IN);
		DbTemplates.delete(env.getConnection(), param);

		if (domain.getIds() != null)
		{
			for (String uid : uids)
			{

				for (int i = 0; i < domain.getIds().length; i++)
				{
					param = new DbParameter("USER_ROLE T1");
					param.setParams("ID", KeyGenerator.getInstance().getKeyId(env.getConnection()), SqlTypes.CHAR);
					param.setParams("USER_ID", uid, SqlTypes.CHAR);
					param.setParams("ROLE_ID", domain.getIds()[i], SqlTypes.CHAR);
					param.setParams("CREATE_USER_ID", env.getLoginUser().getUserId(), SqlTypes.CHAR);
					param.setParams("CREATE_USER", env.getLoginUser().getUserName(), SqlTypes.VARCHAR);
					param.setParams("CREATE_DATE", "sysdate", SqlTypes.CUSTOM);
					param.setParams("UPDATE_USER_ID", env.getLoginUser().getUserId(), SqlTypes.CHAR);
					param.setParams("UPDATE_USER", env.getLoginUser().getUserName(), SqlTypes.VARCHAR);
					param.setParams("UPDATE_DATE", "sysdate", SqlTypes.CUSTOM);
					if (!DbTemplates.insert(env.getConnection(), param))
					{
						env.getConnection().rollback();
						return "0";
					}
				}
			}
		}
		return "1";
	}

	public static List<HashMap<String, String>> users(Environment env, UserRole domain) throws SQLException
	{
		if (StringUtils.isEmpty(domain.getUserIds()))
		{
			return null;
		}
		DbParameter param = new DbParameter("USERS T1");
		param.setSelectFields("T1.ID,T1.NAME");
		param.setWhereParams("T1.ID", param.getInSQL(domain.getUserIds().split(",")), SqlTypes.CUSTOM, WhereTypes.AND, SameTypes.IN);
		return DbTemplates.select(env.getConnection(), param);
	}
}